package com.simon.jkxj.ui.diet;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.simon.jkxj.R;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class QuitSmokingActivity extends AppCompatActivity {
    private Handler timerHandler = new Handler(Looper.getMainLooper());
    private Runnable timerRunnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quit_smoking);
        ImageView btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvDate = findViewById(R.id.tv_date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日", Locale.getDefault());
        String dateStr = dateFormat.format(new Date());
        tvDate.setText(dateStr);
        updateStatus();
        startQuitDaysTimer();
        Button btnCheckin = findViewById(R.id.btn_checkin);
        btnCheckin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QuitSmokingDataManager.checkinToday(QuitSmokingActivity.this);
                updateStatus();
            }
        });
        findViewById(R.id.btn_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new android.content.Intent(QuitSmokingActivity.this, QuitSmokingSettingActivity.class));
            }
        });
    }
    private void updateStatus() {
        TextView tvStatus = findViewById(R.id.tv_status);
        TextView tvStreak = findViewById(R.id.tv_streak);
        TextView tvTotal = findViewById(R.id.tv_total);
        Button btnCheckin = findViewById(R.id.btn_checkin);
        if (QuitSmokingDataManager.isTodayCheckedIn(this)) {
            tvStatus.setText("今日已戒烟打卡");
            btnCheckin.setText("已打卡");
            btnCheckin.setEnabled(false);
        } else {
            tvStatus.setText("今日未打卡");
            btnCheckin.setText("打卡");
            btnCheckin.setEnabled(true);
        }
        tvStreak.setText(String.valueOf(QuitSmokingDataManager.getStreakDays(this)));
        tvTotal.setText(String.valueOf(QuitSmokingDataManager.getTotalDays(this)));
    }
    private void startQuitDaysTimer() {
        final TextView tvQuitDays = findViewById(R.id.tv_quit_days);
        final TextView tvCigCount = findViewById(R.id.tv_cig_count);
        final TextView tvMoney = findViewById(R.id.tv_money);
        final TextView tvTar = findViewById(R.id.tv_tar);
        String startDateStr = QuitSmokingDataManager.getQuitStartDate(this);
        if (startDateStr == null) {
            tvQuitDays.setText("0天0时0分0秒");
            tvCigCount.setText("0支");
            tvMoney.setText("0元");
            tvTar.setText("0mg");
            return;
        }
        try {
            final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
            final long startTime = sdf.parse(startDateStr).getTime();
            timerRunnable = new Runnable() {
                @Override
                public void run() {
                    long now = System.currentTimeMillis();
                    long diff = now - startTime;
                    long days = diff / (24 * 60 * 60 * 1000L);
                    long hours = (diff / (60 * 60 * 1000L)) % 24;
                    long minutes = (diff / (60 * 1000L)) % 60;
                    long seconds = (diff / 1000L) % 60;
                    String timeStr = days + "天" + hours + "时" + minutes + "分" + seconds + "秒";
                    tvQuitDays.setText(timeStr);
                    // 读取设置参数
                    android.content.SharedPreferences prefs = getSharedPreferences("quit_smoking_prefs", MODE_PRIVATE);
                    int cigCount = prefs.getInt("cig_count", 0);
                    float cigPrice = prefs.getFloat("cig_price", 0);
                    int cigPerPack = prefs.getInt("cig_per_pack", 20);
                    int cigTar = prefs.getInt("cig_tar", 0);
                    // 计算累计小时数
                    long totalHours = diff / (60 * 60 * 1000L);
                    // 累计少吸
                    int totalCig = (int) Math.round(cigCount * (totalHours / 24.0));
                    // 节省金钱
                    float totalMoney = cigPerPack > 0 ? (totalCig * 1.0f / cigPerPack) * cigPrice : 0f;
                    // 少吸焦油
                    float totalTar = totalCig * cigTar;
                    tvCigCount.setText(totalCig + "支");
                    tvMoney.setText(String.format("%.2f元", totalMoney));
                    tvTar.setText(String.format("%.1fmg", totalTar));
                    timerHandler.postDelayed(this, 1000);
                }
            };
            timerHandler.post(timerRunnable);
        } catch (Exception e) {
            tvQuitDays.setText("0天0时0分0秒");
            tvCigCount.setText("0支");
            tvMoney.setText("0元");
            tvTar.setText("0mg");
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timerHandler != null && timerRunnable != null) {
            timerHandler.removeCallbacks(timerRunnable);
        }
    }
} 